package com.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * Hello world!
 *
 */
public class SocketServer
{
    public static void main( String[] args ) throws IOException {
        ServerSocket serverSocket =new ServerSocket(9001);
        while (true){
            System.out.println(new Date()+"等待客户端的连接");
            //下一步会阻塞 直到等待客户端的连接
            Socket socket = serverSocket.accept();
            System.out.println(new Date()+"客户端连上了");
            //处理客户端的读写请求
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            System.out.println("收到客户端的数据"+new String(bytes,0,len));
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("服务端响应 success".getBytes());
        }
    }
}
